(function ($)
	{
		function mvsDoc(p_parent)
		{
			this.dirtyFields = 0;
			this.ui_parent = p_parent;
			this.finalCheckBox = $("input.mvsFieldCheckBox", p_parent).mvsFieldCheckBoxPlugin();
			this.commentTextBox = $("input.mvsFieldComment", p_parent).mvsFieldTextBoxPlugin();
			console.assert(this.finalCheckBox.length == 1, "There should be exactly one final field for", p_parent);
			console.assert(this.commentTextBox.length == 1, "There should be exactly one comments field for", p_parent);
			//jQuery.error("some error");
			
			this.fieldChangedDirty = function (e, param)
			{
				if (param)
				{
					if (this.dirtyFields++ == 0)
					{
						$(this.ui_parent).addClass("mvsDirty");
						$(this.ui_parent).trigger("rowDirty", this.dirtyFields);
					}
				}
				else
				{
					if (this.dirtyFields-- == 1)
					{
						$(this.ui_parent).removeClass("mvsDirty");
						$(this.ui_parent).trigger("rowDirty", this.dirtyFields);
					}
				}
				console.info("%o dirty->%s by event at %o of dirty=%s", this.ui_parent, this.dirtyFields, e.target, param);
			}
			.bind(this);
			
			$(p_parent).bind("fieldDirty", this.fieldChangedDirty);
		}
		
		$.fn.mvsDocPlugin = function ()
		{
			return this.each(function ()
				{
					$(this).data("mvsObj", new mvsDoc(this));
				}
			)
		};
		
	}
)(jQuery);

(function ($)
	{
		function mvsFieldCheckBox(p_parent)
		{
			this.originalValue = p_parent.checked;
			this.ui_parent = p_parent;
			this.dirty = false;
			
			this.restoreOriginalValue = function ()
			{
				this.setUIvalue(this.originalValue);
			}
			
			this.setUIvalue = function (p_checked)
			{
				this.ui_parent.checked = p_checked;
				this.uiChanged();
			}
			
			this.uiChanged = function ()
			{
				//console.log(this.ui_parent.id, this.ui_parent.checked, this.originalValue);
				if (this.originalValue == this.ui_parent.checked)
				{
					this.dirty = false;
					$(this.ui_parent).removeClass("mvsDirty");
				}
				else
				{
					this.dirty = true;
					$(this.ui_parent).addClass("mvsDirty");
				}
				$(this.ui_parent).trigger("fieldDirty", this.dirty);
				if (this.ui_parent.checked)
					$(this.ui_parent).trigger("cbSelected");
				//console.log(this.dirty, this.ui_parent.id);
			}
			.bind(this);
			
			$(p_parent).bind("change", this.uiChanged);
		}
		
		$.fn.mvsFieldCheckBoxPlugin = function ()
		{
			return this.each(function ()
				{
					$(this).data("mvsObj", new mvsFieldCheckBox(this));
					//$("input.mvsFieldCheckBox").filter(function(){return $(this).data("mvsObj").dirty===false})
				}
			)
		};
		
	}
)(jQuery);

(function ($)
	{
		function mvsFieldTextBox(p_parent)
		{
			this.originalValue = p_parent.value;
			this.ui_parent = p_parent;
			this.dirty = false;
			
			this.restoreOriginalValue = function ()
			{
				this.setUIvalue(this.originalValue);
			}
			
			this.setUIvalue = function (p_value)
			{
				this.ui_parent.value = p_value;
				this.uiChanged();
			}
			
			this.uiChanged = function ()
			{
				//console.log(this.ui_parent.id, this.ui_parent.checked, this.originalValue);
				var newDirty;
				if (this.originalValue == this.ui_parent.value)
				{
					newDirty = false;
					$(this.ui_parent).removeClass("mvsDirty");
				}
				else
				{
					newDirty = true;
					$(this.ui_parent).addClass("mvsDirty");
				}
				if (newDirty != this.dirty)
				{
					$(this.ui_parent).trigger("fieldDirty", newDirty);
					this.dirty = newDirty;
				}
				//console.log(this.dirty, this.ui_parent.id);
			}
			.bind(this);
			
			$(p_parent).bind("change", this.uiChanged);
		}
		
		$.fn.mvsFieldTextBoxPlugin = function ()
		{
			return this.each(function ()
				{
					$(this).data("mvsObj", new mvsFieldTextBox(this));
					//$("input.mvsFieldCheckBox").filter(function(){return $(this).data("mvsObj").dirty===false})
				}
			)
		};
		
	}
)(jQuery);

$(document).ready(
	function ()
	{
		//$("div.mvsDocs").mvsDocsPlugin();
		//$("input.mvsFieldCheckBox").mvsFieldCheckBoxPlugin();
		//$("input.mvsFieldComment").mvsFieldTextBoxPlugin();
		$("tr.mvsDoc").mvsDocPlugin();
		$("td").mouseenter(function ()
			{
				$(this).trigger("fieldChanged", "paparm here")
			}
		)
	}
);

 